<template>
  <div>
    <el-dialog title="投标" :visible="dialogVisible" @close="handleClose">
      <el-form ref="AddBid" :model="editBidData" size="small" label-width="120px">
        <el-form-item label="供应商名">
          <el-input
            v-model.trim="editBidData.supplierName"
            :readonly="isReadOnly"
            placeholder="供应商名"
          />
        </el-form-item>
        <el-form-item label="投标说明">
          <el-input v-model.trim="editBidData.remark" placeholder="投标说明" />
        </el-form-item>
        <el-form-item label="总价（含税）">
          <el-input v-model.number="editBidData.total" placeholder="总价（含税）" />
        </el-form-item>

        <el-table
          ref="filesTable"
          :v-if="upLoadFilesShowFlag"
          :data="upLoadFiles"
          :row-style="{height:'40px'}"
          :cell-style="{padding:'0px'}"
          :show-header="false"
          height="155px"
          style="width: 100%;"
        >
          <el-table-column prop="fileName" label="fileName">
            <template slot-scope="scope">
              <el-button
                icon="el-icon-paperclip"
                type="text"
                @click="downLoadClick(scope.row)"
              >{{ scope.row.fileName }}</el-button>
            </template>
          </el-table-column>
          <el-table-column width="100" align="center">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                style="color:orangered;"
                @click="removeAttachFile(scope.$index)"
              >{{ $t('button.Delete') }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-form-item>
          <el-upload v-show="false" :on-success="onsuccess" class="upload-demo" :action="actionData">
            <el-button v-show="false" ref="upload1" class="btnUpload" />
          </el-upload>
          <el-button type="primary" size="medium" style="margin-left:10px" plain @click="importDoc">上传附件</el-button>
          <el-button
            type="primary"
            plain
            size="medium"
            @click="biddingClick"
          >{{ $t('button.Confirm') }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { Bidding } from '@/api/SRM'
import elDragDialog from '@/components/el-drag-dialog' // base on element-ui
import { Message } from 'element-ui'
import { getUserInfo } from '@/utils/auth.js'
import config from '@/config'
export default {
  name: 'AddBidDialog',
  directives: { elDragDialog },
  props: {
    editBidDialogFlag: {
      type: Boolean,
      default: false
    },
    editBidData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      upLoadFilesShowFlag: false,
      dialogVisible: false,
      isReadOnly: false,
      // editBidData: {
      // },
      upLoadFiles: [],
      // 上传地址
      actionData: `http://${document.location.hostname}:${document.location.port}/File/Upload`
    }
  },
  watch: {
    editBidDialogFlag() {
      this.dialogVisible = this.editBidDialogFlag
    }
  },
  mounted() {},
  created() {
    const userInfo = getUserInfo()
    if (userInfo != null) {
      this.isReadOnly = true
      // this.editBidData = editBidData
      this.editBidData.supplierName = userInfo.userName
      // this.editBidData.publisherMailAddr = userInfo.mailAccount;
    } else {
      this.isReadOnly = false
    }
  },
  methods: {
    async submit() {
      try {
        this.editBidData.Files = this.upLoadFiles.map(item => item.id)
        console.log(this.editBidData)
        // let res = await Bidding(this.editBidData);
        Message.success('创建成功')
        this.dialogVisible = false
      } catch (error) {
        console.log('AddBid->submit', error.message)
        Message.error('创建失败')
      }
    },
    handleClose() {
      this.$emit('updateValue')
      this.$emit('update:editBidDialogFlag', false)
    },
    // 上传
    async onsuccess(response, file, fileList) {
      console.log(response, file, fileList, 'response, file, fileList')
      this.upLoadFiles.push(response.data)
      Message.success('上传成功')
    },
    // 判断是否勾选文件
    importDoc() {
      this.$refs['upload1'].$el.click() // 打开上传文件弹窗
    },
    /** 下载文件 */
    async downLoadClick(data) {
      const baseURL =
        process.env.NODE_ENV === 'development'
          ? config.baseUrl.dev
          : config.baseUrl.build
      window.open(`${baseURL}File/Download?id=${data.id}`)
    },
    /** 移除当前附件 */
    removeAttachFile(index) {
      this.$confirm('确认移除当前文件吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.upLoadFiles.splice(index, 1)
          this.upLoadFilesShowFlag = this.upLoadFiles.length > 0
          console.log('upLoadFilesShowFlag', this.upLoadFilesShowFlag)
          Message.success('操作成功')
        })
        .catch(() => {})
    },
    async biddingClick() {
      try {
        this.editBidData.files = this.upLoadFiles.map(item => item.id)
        console.log(this.editBidData)
        const res = await Bidding(this.editBidData)
        Message.success('投标成功')
      } catch (error) {
        Message.error('投标失败')
      }
    }
  }
}
</script>

<style>
</style>
